﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
using System.IO;

public partial class FileUploadExtension : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
		if (!IsPostBack)
		{
			//開啓網站的Web.config組態檔
			Configuration config = WebConfigurationManager.OpenWebConfiguration(this.Request.ApplicationPath);
			//取得system.web/httpRuntime組態區段
			HttpRuntimeSection myHttpRuntimeSection = (HttpRuntimeSection)config.GetSection("system.web/httpRuntime");
			//取得MaxRequestLength設定值大小
			txtFileSize.Text = "(檔案上載總和最大限制為" + string.Format("{0:N0}", Convert.ToInt32(myHttpRuntimeSection.MaxRequestLength)) + "KB大小)";
			FileUpload1.Focus();
		}
    }

	//檔案上載
	protected void btnUpload_Click(object sender, EventArgs e)
	{
		txtMsg.Text = "";
		try
		{
			Upload(FileUpload1);
			Upload(FileUpload2);
			Upload(FileUpload3);
			Upload(FileUpload4);
			Upload(FileUpload5);
			Upload(FileUpload6);
			Upload(FileUpload7);
			Upload(FileUpload8);
			Upload(FileUpload9);
			Upload(FileUpload10);

			txtMsg.Text = "完成檔案上載如下：<BR>" + ViewState["Uploads"].ToString();
			ViewState["Uploads"] = null;
		}
		catch (Exception ex)
		{
			txtMsg.Text = ex.Message;
		}
	}

	//上載檔案方法
	private void Upload(FileUpload myFileUpload)
	{
		//是否允許上載
		bool fileAllow = false;
		//設定允許上載的延伸檔名類型
		string[] allowExtensions ={ ".doc",".xls",".ppt",".jpg",".gif"};

		//取得網站根目錄路徑
		string path = HttpContext.Current.Request.MapPath("~/");
		//檢查是否有檔案
		if (myFileUpload.HasFile)
		{
			//取得上載檔案之延伸檔名，並轉換成小寫字母
			string fileExtension = System.IO.Path.GetExtension(myFileUpload.FileName).ToLower();
			//檢查延伸檔名是否符合限定類型
			for (int i = 0; i < allowExtensions.Length; i++)
			{
				if (fileExtension == allowExtensions[i])
				{
					fileAllow = true;
				}
			}

			if (fileAllow)
			{
				try
				{
					//儲存檔案到磁碟
					myFileUpload.SaveAs(path + myFileUpload.FileName);
					ViewState["Uploads"] += myFileUpload.PostedFile.FileName + "<br>";
				}
				catch (Exception ex)
				{
					txtMsg.Text += ex.Message;
				}
			}
			else
			{
				ViewState["Uploads"] += "不允許上載：" + myFileUpload.PostedFile.FileName + "<br>";
			}
		}
		else
		{
			//檢查至少必須指定一個上載檔案
			if (hasFile())
			{
				txtMsg.Text = "必須指定檔案！";
			}
		}
	}

	//檢查是否有檔案，若十個FileUpload控制項無指定檔案則回傳false
	private bool hasFile()
	{
		//status為檢查FileUpload是否含有檔案，預設為false，若有檔案則為true
		bool status = false;

		status = FileUpload1.HasFile && FileUpload2.HasFile && FileUpload3.HasFile && FileUpload4.HasFile && FileUpload5.HasFile && FileUpload6.HasFile && FileUpload7.HasFile && FileUpload8.HasFile && FileUpload9.HasFile && FileUpload10.HasFile;

		/*以上寫法等同下面寫法
		status = FileUpload1.HasFile;
		status = FileUpload2.HasFile;
		status = FileUpload3.HasFile;
		status = FileUpload4.HasFile;
		status = FileUpload5.HasFile;
		status = FileUpload6.HasFile;
		status = FileUpload7.HasFile;
		status = FileUpload8.HasFile;
		status = FileUpload9.HasFile;
		status = FileUpload10.HasFile;
		 * */
		return status;
	}
}
